package com.quanquan.user.message;

import com.quanquan.user.service.SignInService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Objects;

/**
 * 功能描述:处理连续签到的队列
 *
 * @ClassName: MqSignInContinuousReceiver
 * @Author: xiaoqianh丶
 * @Date: 2019-04-05 23:02
 * @Version: V1.0
 */
@Slf4j
@Component
public class MqSignInContinuousReceiver {

    private org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());

    private final SignInService signInService;

    @Autowired
    public MqSignInContinuousReceiver(SignInService signInService) {
        this.signInService = signInService;
    }

    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(value = "signInContinuous",
                    durable = "true"),
            exchange = @Exchange(value = "signIn.exchange",
                    type = "topic"),
            ignoreDeclarationExceptions = "true",
            key = "signIn.*")})
    @RabbitHandler
    public void checkSignContinuous(Message message, Channel channel) throws IOException {
        //会员id
        String userId = Objects.requireNonNull(message.getHeaders().get("userId")).toString();
        //连续签到积分奖励数
        String bonusPoints = Objects.requireNonNull(message.getHeaders().get("bonusPoints")).toString();
        //连续签到天数
        String consecutiveDay = Objects.requireNonNull(message.getHeaders().get("consecutiveDay")).toString();
        //手工ack
        long deliveryTag = Long.parseLong(Objects.requireNonNull(message.getHeaders().get(AmqpHeaders.DELIVERY_TAG)).toString());
        channel.basicAck(deliveryTag, false);

    }
}